package com.example.mofei.myapplication;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

/**
 * Created by mofei on 2017/10/23.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> implements ItemMoveListener {
    private List<String> contentList;
    private OnItemClickListener itemClickListener;
    private ItemDragListener itemDragListener;

    public List<String> getContentList() {
        return contentList;
    }

    public void setContentList(List<String> contentList) {
        this.contentList = contentList;
    }


    public RecyclerViewAdapter(){

    }

    public RecyclerViewAdapter(List<String> contentList,ItemDragListener itemDragListener){
        this.contentList=contentList;
        this.itemDragListener=itemDragListener;
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView txtTitle=null;
        ImageView ivDrag=null;
        public MyViewHolder(View itemView) {
            super(itemView);
            ivDrag=(ImageView) itemView.findViewById(R.id.ivDrag);
            txtTitle=(TextView)itemView.findViewById(R.id.txtTitle);
        }
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (itemClickListener!=null){
//            holder.tv.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    itemClickListener.clickListener(view,position);
//                }
//            });
            holder.txtTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListener.clickListener(view,position);
                }
            });
            holder.ivDrag.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    itemDragListener.onStartDrags(holder);
                    return false;
                }
            });
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder=new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate( R.layout.recycler_item, parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.txtTitle.setText(contentList.get(position));
    }

    @Override
    public int getItemCount() {
        if (contentList!=null){
            return contentList.size();
        }
        return 0;
    }

    public void addItem(String content,int position){
        contentList.add(position,content);
        this.notifyItemInserted(position);
        this.notifyItemRangeChanged(position,contentList.size()-position);
    }

    public void updateItem(String content,int position){
        contentList.set(position,content);
        this.notifyItemChanged(position);
    }

    public interface OnItemClickListener{
        void clickListener(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.itemClickListener=listener;
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        //1.交换数据
        Collections.swap(contentList,fromPosition,toPosition);
        //2.刷新
        this.notifyItemMoved(fromPosition,toPosition);
        return true;
    }

    @Override
    public boolean onItemRemove(int position) {
        //1.删除数据
        contentList.remove(position);
        //2.刷新
        this.notifyItemRemoved(position);
        return true;
    }
}
